package auth

import (
	"context"
	"errors"
	"strings"
	"time"

	"system-admin/config"
	"system-admin/config/mysql"
	"system-admin/internal/models"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/logx"
	"gorm.io/gorm"
)

type AuthRuleTree struct {
	models.AuthRule
	Children []AuthRuleTree `json:"children" gorm:"-"` // 子权限
}

// UpdateAuthRules 初始化权限规则
func UpdateAuthRules(db *gorm.DB, authRuleTree AuthRuleTree, PID string) error {
	var authRule models.AuthRule
	// 创建权限规则（根节点除外）
	if authRuleTree.ID != "1" {
		authRule.PID = PID
		query := db.Model(&models.AuthRule{}).
			Where(models.AuthRule{
				Name:  authRuleTree.Name,
				Value: authRuleTree.Value,
				PID:   authRule.PID,
			}).
			Assign(models.AuthRule{
				Title:    authRuleTree.Title,
				RuleType: authRuleTree.RuleType,
				Order:    authRuleTree.Order,
			}).
			Select(
				"id", // 没有这个id，会出现bug，提示pid不能为空？？？
				"name",
				"value",
				"pid",
				"title",
				"rule_type",
				"order",
			).
			FirstOrCreate(&authRule)

		if query.Error != nil {
			logx.Error(context.Background(), "initAuthRules", logx.Err(query.Error))
		}

		if query.RowsAffected <= 0 {
			return errors.New("create/update auth rule failed")
		}
		authRuleTree.AuthRule = authRule
	}

	// 递归创建子权限
	for _, childAuthRule := range authRuleTree.Children {
		UpdateAuthRules(db, childAuthRule, authRuleTree.ID)
	}
	return nil
}

// RuleTree初始化权限的定义
var AuthRulesDefined = AuthRuleTree{}

func init() {
	AuthRulesDefined = AuthRuleTree{
		AuthRule: models.AuthRule{
			ID:       "1",
			PID:      "-1",
			Title:    "root",
			Name:     "root",
			RuleType: 0,
		},
		Children: []AuthRuleTree{
			systemAdminAuthRulesDefined,
			deviceAdminAuthRulesDefined,
			saleAdminAuthRulesDefined,
			marketSupportAdminRulesDefined,
			advertisingAdminRulesDefined,
			interactiveAdminRulesDefined,
		},
	}
	if config.Config.ProjectName != "sg" {
		AuthRulesDefined.Children = append(AuthRulesDefined.Children, minAppAdminRuleDefined)
	}
}

// 售卖后台
var saleAdminAuthRulesDefined = AuthRuleTree{
	AuthRule: models.AuthRule{
		Title:    "零售平台",
		Name:     "sale-admin",
		RuleType: 1,
		Order:    1,
	},
	Children: []AuthRuleTree{
		{
			AuthRule: models.AuthRule{
				Title:    "首页",
				Name:     "home",
				RuleType: 2,
				Order:    1,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "商品管理",
				Name:     "goods",
				RuleType: 2,
				Order:    2,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "商品列表",
						Name:     "goodsList",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "商品标签",
						Name:     "classList",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "背景配置",
						Name:     "background",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "商品脚标",
						Name:     "corner-label",
						RuleType: 2,
						Order:    4,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "设备补货",
				Name:     "restocked",
				RuleType: 2,
				Order:    3,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "设备补货",
						Name:     "goodsStock",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "补货记录",
						Name:     "restockedLog",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "历史库存",
						Name:     "history-stock",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "订单管理",
				Name:     "order",
				RuleType: 2,
				Order:    4,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "订单列表",
						Name:     "orderList",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "收入流水",
						Name:     "incomeList",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "退款流水",
						Name:     "refundList",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "退款分析",
						Name:     "refund-analysis",
						RuleType: 2,
						Order:    4,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "数据统计",
				Name:     "statistics",
				RuleType: 2,
				Order:    5,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "数据筛查",
						Name:     "realTimeMore",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "订单概览",
						Name:     "orderDesc",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "商品概览",
						Name:     "goodsDesc",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "商品统计",
						Name:     "goodsDescCount",
						RuleType: 2,
						Order:    4,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "数据对比",
						Name:     "dataVs",
						RuleType: 2,
						Order:    5,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "复购用户",
						Name:     "repeatUser",
						RuleType: 2,
						Order:    6,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "客户画像统计",
						Name:     "drawFace",
						RuleType: 2,
						Order:    7,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "曝光统计",
						Name:     "datePerson",
						RuleType: 2,
						Order:    8,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "客群数据",
				Name:     "customer-statistics",
				RuleType: 2,
				Order:    6,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "曝光数据",
						Name:     "facecount-statistics",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "凝视数据",
						Name:     "starecount-statistics",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "画像统计",
						Name:     "draw-face",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "运营报告",
				Name:     "report",
				RuleType: 2,
				Order:    7,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "运营数据",
						Name:     "report-index",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "运营热力图",
						Name:     "heat-map",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "报告列表",
						Name:     "report-config",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "空货数据",
						Name:     "zero-stock",
						RuleType: 2,
						Order:    4,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "异常设备",
						Name:     "abnormal-device",
						RuleType: 2,
						Order:    5,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "运营事件",
				Name:     "event",
				RuleType: 2,
				Order:    8,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "运营事件",
						Name:     "report-event",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "事件配置",
						Name:     "report-event-type",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "事件分析",
						Name:     "report-event-desc",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "商户管理",
				Name:     "merchant",
				RuleType: 2,
				Order:    9,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "系统设置",
				Name:     "system-setting",
				RuleType: 2,
				Order:    10,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "支付设置",
						Name:     "payment-setting",
						RuleType: 2,
						Order:    0,
					},
					Children: []AuthRuleTree{
						{
							AuthRule: models.AuthRule{
								Title:    "编辑",
								Value:    "edit",
								RuleType: 3,
								Order:    0,
							},
						},
						{
							AuthRule: models.AuthRule{
								Title:    "查看",
								Value:    "read",
								RuleType: 3,
								Order:    1,
							},
						},
					},
				},
			},
		},
	},
}

// 小程序后台
var minAppAdminRuleDefined = AuthRuleTree{
	AuthRule: models.AuthRule{
		Title:    "小程序平台",
		Name:     "mini-app-admin",
		RuleType: 1,
		Order:    2,
	},
	Children: []AuthRuleTree{
		{
			AuthRule: models.AuthRule{
				Title:    "门店配置",
				Name:     "device",
				RuleType: 2,
				Order:    1,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "门店列表",
						Name:     "device-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "展示分类",
						Name:     "goods-class",
						RuleType: 2,
						Order:    2,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "订单管理",
				Name:     "order",
				RuleType: 2,
				Order:    2,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "会员管理",
				Name:     "member",
				RuleType: 2,
				Order:    3,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "渠道配置",
				Name:     "promotion-channel",
				RuleType: 2,
				Order:    4,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "评测管理",
				Name:     "review",
				RuleType: 2,
				Order:    5,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "评测列表",
						Name:     "review-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "待审核列表",
						Name:     "pending-review-List",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "审核记录",
						Name:     "review-log",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "线上活动",
				Name:     "online-event",
				RuleType: 2,
				Order:    6,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "创建活动",
						Name:     "create-online-event",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "活动列表",
						Name:     "online-event-list",
						RuleType: 2,
						Order:    2,
					},
				},
			},
		},
	},
}

// 设备后台
var deviceAdminAuthRulesDefined = AuthRuleTree{
	AuthRule: models.AuthRule{
		Title:    "设备平台",
		Name:     "device-admin",
		RuleType: 1,
		Order:    3,
	},
	Children: []AuthRuleTree{
		{
			AuthRule: models.AuthRule{
				Title:    "设备状态",
				Name:     "device-status",
				RuleType: 2,
				Order:    1,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "设备状态",
						Name:     "device-status-index",
						RuleType: 2,
						Order:    1,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "设备管理",
				Name:     "device",
				RuleType: 2,
				Order:    2,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:       "设备列表",
						Name:        "device-list",
						DBTableName: "device-admin.device",
						RuleType:    2,
						Order:       2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:       "位置管理",
						Name:        "location",
						DBTableName: "device-admin.location",
						RuleType:    2,
						Order:       3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "远程维护",
				Name:     "remote-control",
				RuleType: 2,
				Order:    3,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "导航管理",
						Name:     "remote-control-index",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "售卖屏管理",
						Name:     "remote-control-97",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "媒体屏管理",
						Name:     "remote-control-98",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "远程监控",
				Name:     "remote-monitor",
				RuleType: 2,
				Order:    4,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "设备监控",
						Name:     "remote-monitor-index",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "状态监控",
						Name:     "remote-monitor-status",
						RuleType: 2,
						Order:    2,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "告警配置",
				Name:     "alarm-setting",
				RuleType: 2,
				Order:    5,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "终端管理",
				Name:     "terminal",
				RuleType: 2,
				Order:    6,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "终端列表",
						Name:     "terminal-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "升级包",
						Name:     "upgrade-package",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "升级计划",
						Name:     "upgrade-schedule",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "webshell",
						Name:     "webshell",
						RuleType: 2,
						Order:    4,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "开发日志",
				Name:     "devlog",
				RuleType: 2,
				Order:    7,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "异常事件",
						Name:     "event-log",
						RuleType: 2,
						Order:    1,
					},
				},
			},
		},
	},
}

// 业务活动
var marketSupportAdminRulesDefined = AuthRuleTree{
	AuthRule: models.AuthRule{
		Title:    "业务活动",
		Name:     "market-support",
		RuleType: 1,
		Order:    4,
	},
	Children: []AuthRuleTree{
		// {
		// 	AuthRule: models.AuthRule{
		// 		Title:    "首页",
		// 		Name:     "home",
		// 		RuleType: 2,
		// 		Order:    1,
		// 	},
		// },
		{
			AuthRule: models.AuthRule{
				Title:    "场内导航",
				Name:     "nav",
				RuleType: 2,
				Order:    2,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "品牌库",
						Name:     "brand",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "导航管理",
						Name:     "navigation",
						RuleType: 2,
						Order:    2,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "开放数据",
				Name:     "location",
				RuleType: 2,
				Order:    4,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "位置列表",
						Name:     "location-list",
						RuleType: 2,
						Order:    1,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "重要提示",
				Name:     "statement",
				RuleType: 2,
				Order:    5,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "页面提示",
						Name:     "statement-list",
						RuleType: 2,
						Order:    1,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "兑换码",
				Name:     "cdkey",
				RuleType: 2,
				Order:    6,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "兑换码列表",
						Name:     "cdkeyList",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "失效兑换码",
						Name:     "invalidList",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "生成记录",
						Name:     "recordList",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "留资活动",
				Name:     "activity",
				RuleType: 2,
				Order:    7,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "活动列表",
						Name:     "activityList",
						RuleType: 2,
						Order:    1,
					},
				},
			},
		},
	},
}

// 广告投放
var advertisingAdminRulesDefined = AuthRuleTree{
	AuthRule: models.AuthRule{
		Title:    "广告投放",
		Name:     "advertising-admin",
		RuleType: 1,
		Order:    5,
	},
	Children: []AuthRuleTree{
		{
			AuthRule: models.AuthRule{
				Title:    "计划管理",
				Name:     "publish-schedule",
				RuleType: 2,
				Order:    1,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "素材管理",
				Name:     "material",
				RuleType: 2,
				Order:    2,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "图片素材",
						Name:     "img-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "视频素材",
						Name:     "video-list",
						RuleType: 2,
						Order:    2,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "审核管理",
				Name:     "audit",
				RuleType: 2,
				Order:    3,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "素材审核",
						Name:     "material-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "投放审核",
						Name:     "publish-list",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "素材审核记录",
						Name:     "material-log",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "投放审核记录",
						Name:     "publish-log",
						RuleType: 2,
						Order:    4,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "商户投放管理",
				Name:     "publish-device",
				RuleType: 2,
				Order:    4,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "商户列表",
						Name:     "merchant-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "视频素材",
						Name:     "group-list",
						RuleType: 2,
						Order:    2,
					},
				},
			},
		},
	},
}

// 互动平台
var interactiveAdminRulesDefined = AuthRuleTree{
	AuthRule: models.AuthRule{
		Title:    "互动平台",
		Name:     "interactive-admin",
		RuleType: 1,
		Order:    6,
	},
	Children: []AuthRuleTree{
		{
			AuthRule: models.AuthRule{
				Title:    "抽奖活动",
				Name:     "activity",
				RuleType: 2,
				Order:    1,
			},
			// activity-create   activity-member  activity-statistics
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "活动列表",
						Name:     "activity-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "活动创建",
						Name:     "activity-create",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "会员数据",
						Name:     "activity-member",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "活动数据",
						Name:     "activity-statistics",
						RuleType: 2,
						Order:    4,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "优惠码",
				Name:     "coupon",
				RuleType: 2,
				Order:    2,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "优惠码列表",
						Name:     "coupon-list",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "新增优惠码",
						Name:     "coupon-create",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "码库",
						Name:     "coupon-code",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "定制身份",
				Name:     "material",
				RuleType: 2,
				Order:    3,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "身份列表",
						Name:     "theme",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "素材管理",
						Name:     "material-folder",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "素材审核列表",
						Name:     "material-audit",
						RuleType: 2,
						Order:    3,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "本地词库",
						Name:     "local-qa",
						RuleType: 2,
						Order:    4,
					},
				},
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:    "商场服务",
				Name:     "mall-service",
				RuleType: 2,
				Order:    4,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "开通服务",
						Name:     "enable-service",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "服务设置",
						Name:     "service-setting",
						RuleType: 2,
						Order:    2,
					},
				},
			},
		},
	},
}

// 系统后台
var systemAdminAuthRulesDefined = AuthRuleTree{
	AuthRule: models.AuthRule{
		Title:    "系统后台",
		Name:     "system-admin",
		RuleType: 1,
		Order:    7,
	},
	Children: []AuthRuleTree{
		{
			AuthRule: models.AuthRule{
				Title:       "商户管理",
				Name:        "merchant",
				RuleType:    2,
				DBTableName: "system-admin.merchant",
				Order:       1,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:       "用户管理",
				Name:        "user",
				RuleType:    2,
				DBTableName: "system-admin.user",
				Order:       2,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:       "角色管理",
				Name:        "role",
				RuleType:    2,
				DBTableName: "system-admin.role",
				Order:       3,
			},
		},
		{
			AuthRule: models.AuthRule{
				Title:       "告警管理",
				Name:        "alarm",
				RuleType:    2,
				DBTableName: "system-admin.role",
				Order:       4,
			},
			Children: []AuthRuleTree{
				{
					AuthRule: models.AuthRule{
						Title:    "通知配置",
						Name:     "alarm-notify-setting",
						RuleType: 2,
						Order:    1,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "通知记录",
						Name:     "alarm-record",
						RuleType: 2,
						Order:    2,
					},
				},
				{
					AuthRule: models.AuthRule{
						Title:    "告警记录",
						Name:     "alarm-record-all",
						RuleType: 2,
						Order:    3,
					},
				},
			},
		},
	},
}

// DeleteAuthRule 删除路由
// routerPath 路由
// 如 device-admin/map，先找到device-admin的id，在查找该id下的map的id，再进行删除
func DeleteAuthRule(routerPath string) error {
	routerPath = strings.Trim(routerPath, "/")
	routerPathArr := strings.Split(routerPath, "/")
	// 确定要删除的权限id，逐级遍历路由各节点
	pid := "1"
	var deletedRouterID string
	for _, path := range routerPathArr {
		var rule models.AuthRule
		rowsAffected := mysql.NewDB().Model(&models.AuthRule{}).
			Where("`value` = ? OR name = ?", path, path).
			Where("pid = ?", pid).
			Take(&rule).RowsAffected
		if rowsAffected == 1 {
			deletedRouterID = rule.ID
			pid = rule.ID
		} else {
			return errorx.New("不存在的权限", -1)
		}
	}
	// 删除路由下的所有子路由或权限
	if deletedRouterID != "" {
		// 删除权限规则
		mysql.NewDB().Model(&models.AuthRule{}).Where("id = ?", deletedRouterID).Update("deleted_at", time.Now().Unix())
		mysql.NewDB().Model(&models.AuthRule{}).Where("pid = ?", deletedRouterID).Update("deleted_at", time.Now().Unix())
		// 删除角色权限关联
		var childAuthRuleIDs []string
		mysql.NewDB().Model(&models.AuthRule{}).Where("pid = ?", deletedRouterID).Pluck("id", &childAuthRuleIDs)
		childAuthRuleIDs = append(childAuthRuleIDs, deletedRouterID)
		mysql.NewDB().Model(&models.RoleAuthRule{}).Where("rule_id IN ?", childAuthRuleIDs).Update("deleted_at", time.Now().Unix())
	}
	return errors.New("DeleteAuthRule failed")
}

// RenameAuthRule 重命名路由名称
// routerPath 要重命名的路由前缀，包括要重命名的路由名称
func RenameAuthRule(routerPath string, newName string) error {
	routerPath = strings.Trim(routerPath, "/")
	routerPathArr := strings.Split(routerPath, "/")
	// 确定要删除的权限id，逐级遍历路由各节点
	pid := "1"
	for index, path := range routerPathArr {
		var rule models.AuthRule
		rowsAffected := mysql.NewDB().Model(&models.AuthRule{}).
			Where("name = ? AND rule_type = ?", path, pid).
			Take(&rule).RowsAffected
		if rowsAffected == 1 && index == len(routerPathArr)-1 {
			rowsAffected := mysql.NewDB().Model(&models.AuthRule{}).Where("id = ?", rule.ID).Update("name", newName).RowsAffected
			if rowsAffected == 1 {
				return nil
			}
		}
	}
	return errors.New("RenameAuthRule failed")
}
